package com.atguigu.gmall.order.config;


import com.atguigu.gmall.common.config.MybatisPlusConfig;
import com.atguigu.gmall.feign.AppFeignConfig;
import com.atguigu.gmall.mq.AppRabbitConfig;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@EnableFeignClients(basePackages = {
        "com.atguigu.gmall.feign.pay",
        "com.atguigu.gmall.feign.warn",
        "com.atguigu.gmall.feign.product",
        "com.atguigu.gmall.feign.cart"
})
@Import({AppFeignConfig.class, MybatisPlusConfig.class, AppRabbitConfig.class})
@Configuration
public class OrderConfig {
    /**
     * corePoolSize
     * keepAliveTime
     * maximumPoolSize
     * maximumPoolSize
     * workQueue
     * threadFactory 用自己的线程工厂，有uuid
     * RejectedExecutionHandler
     * @return
     */
    @Bean("corePool")
    public ThreadPoolExecutor executor(ThreadConfigProperties properties){
        return new ThreadPoolExecutor(
                properties.core,
                properties.max,
                properties.keepAlive,
                TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>(properties.queueLength),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy()
        );
    }

    @Data
    @Component
    @ConfigurationProperties(prefix = "order-service.thread")
    class ThreadConfigProperties{
        private Integer core;
        private Integer max;
        private Long keepAlive;
        private Integer queueLength;
    }


}
